//
//  YWDropDownRunTimeCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/15.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWDropDownRunTimeCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "YWEquipment.h"
#import "YWEquipmentType.h"
#import "UIView+Extension.h"
#import "YWPopView.h"

@interface YWDropDownRunTimeCell()<YWPopViewDelegate>
@property (nonatomic, strong) UIButton * dropDownButton;
@property (nonatomic, strong) UILabel * parameterNameLabel;
@property (nonatomic, strong) YWPopView *optionPopView;
@property (nonatomic, strong) YWEquipment *currentEquipment;
@end

@implementation YWDropDownRunTimeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.textLabel.font = kContentFont;
        [self setUpSubviews];
    }
    
    return self;
}

-(void)setRunTimeParameter:(YWRunTimeParameter *)runTimeParameter{
    _runTimeParameter = runTimeParameter;
    if (runTimeParameter && runTimeParameter.fieldName.length>0) {
        _parameterNameLabel.text = _runTimeParameter.fieldName;
        _parameterNameLabel.hidden = NO;
        self.textLabel.hidden = YES;
    }else{
        self.textLabel.text = @"无参数";
        self.textLabel.hidden = NO;
    }
    if (runTimeParameter) {
        if (runTimeParameter.fieldOptionValue.fieldOptionText.length) {
            [_dropDownButton setTitle:runTimeParameter.fieldOptionValue.fieldOptionText?:@"" forState:UIControlStateNormal];
        }
    }
}

- (void)setUpSubviews{
    [self.parameterNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.bottom.mas_equalTo(self.dropDownButton.mas_top).offset(-5);
    }];
    
    [self.dropDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
//        make.bottom.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(30);
    }];
}

- (UILabel *)parameterNameLabel{
    if (!_parameterNameLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _parameterNameLabel = label;
    }
    return _parameterNameLabel;
}

- (UIButton *)dropDownButton{
    if (!_dropDownButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"点击选择" forState:UIControlStateNormal];
        button.titleLabel.font = kContentFont;
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor lightGrayColor].CGColor;
        button.layer.borderWidth = 1.0;
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4.0;
        [button addTarget:self action:@selector(clickSelectOptions:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        _dropDownButton = button;
    }
    return _dropDownButton;
}

-(YWPopView *)optionPopView{
    if (!_optionPopView) {
        YWPopView *popView = [[YWPopView alloc]init];
        popView.delegate = self;
        _optionPopView = popView;
    }
    return _optionPopView;
}

- (void)clickSelectOptions:(UIButton*)sender{
    self.optionPopView.optionArray = _runTimeParameter.options;
    [self.optionPopView show];
}

-(void)popRuntimeOption:(YWEquipmentFieldOption *)option{
    _runTimeParameter.fieldOptionValue = option;
    [_dropDownButton setTitle:option.fieldOptionText?:@"" forState:UIControlStateNormal];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.textLabel.x = kMarginW;
}

@end
